// packageStore/pages/profile/userSetting/userSetting.js
const app = getApp()
import {
  config,
  wholesaleSettings
} from "../../../../js/modules/gloab"
import authorize from '../../../../js/modules/authorize.js'
import {
  isEmpty
} from "../../../../js/newUtil"
import {
  profile,
  qywx,
  finance
} from "../../../../js/modules/store.js"
import {
  api,
  util,
  dialog,
  storage
} from '../../../../js/util'
const componentBehavior = require('../../../../js/common/ComponentBehavior')

Page({
  behaviors: [componentBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false, // 是否已登录
    storeInfo: {}, // 用户信息
    EnableSubaccount: 0, // 是否主体可使用主账户
    Initiator: 0,
    IsStoreManagerAccount: -1, //判断子账号
    DisableSubaccount: false,
    salesman: false, // 是否显示我的业务员
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getAuthorityConfig()
    this.getWholesaleConfig()
    this.getFace()
    this.showSalesman()
    this.setData({
      imgDomain: api.imgDomain,
      enableOss: api.enableOss,
      mainColor: api.titleColor || "#e56b14", //主题色
    })
    wx.setNavigationBarColor({
      backgroundColor: api.titleColor || "#e56b14", //主题色,
      frontColor: '#ffffff',
    });
    let storeInfo = authorize.getEntity()
    if (!isEmpty(storeInfo) && !isEmpty(storeInfo.role)) {
      let role = storeInfo.role || {}
      let manager = storeInfo.manager || {}
      this.setData({
        storeInfo: storeInfo,
        Initiator: manager.Initiator || 0,
        DisableSubaccount: role.DisableSubaccount == '1' ? true : false
      })
    }
  },
  onShow() {
    this.setData({
      isLogin: authorize.isLogin("store")
    })
    // 展示错误日志
    let systemErrorLog = util.getErrorLog()
    this.setData({
      showSystemErrorLog: systemErrorLog.length > 0 ? true : false
    })
  },
  getAuthorityConfig() {
    var that = this;
    var fkid = app.dns.proprietorId;
    var fkflag = app.dns.proprietor;
    var entity = authorize.getEntity() || authorize.getDefault()
    let manager = entity.manager || {}
    config.getAuthorityConfig(
      fkflag,
      fkid,
      manager.Token,
      function (res) {
        if (res.Success) {
          let data = res.Content
          that.setData({
            EnableSubaccount: data.EnableSubaccount
          });
        }
      },
      true
    )
  },
  /**加载商品类的配置项参数 */
  getWholesaleConfig() {
    var that = this
    var fkid = app.dns.proprietorId
    var fkflag = app.dns.proprietor
    config.getWholesaleConfigs(
      fkid,
      fkflag,
      "Wholesale",
      function (model) {
        that.setData({
          IsStoreManagerAccount: model.IsStoreManagerAccount
        })
        wholesaleSettings.setConfig(model)
      });
  },
  /**
   * 获取用户头像
   */
  getFace() {
    var that = this
    var data = {
      proprietor: app.dns.proprietor,
      proprietorId: app.dns.proprietorId,
    }
    config.getAvatar(data, function (res) {
      if (res.Success) {
        that.setData({
          userFace: res.Content.Face || res.Content.Logo
        })
      }
    })
  },
  //业务员显示
  showSalesman() {
    var that = this
    profile.salesman(function (res) {
      if (res.Success) {
        that.setData({
          salesman: res.Content == 'true'
        })
      }
    });
  },
  /**
   * 退出登录
   */
  logout: function () {
    //退出登录
    profile.logOut()
    profile.loginOut()
    if (app.dns.proprietor != app.dns.fkFlag) {
      app.dns.proprietorId = 0
    }
    app.globalData.index.isReload = true
    //跳转到登录页面
    authorize.redirect('store')
  },
    /**
   * 显示复制错误日志弹窗
   */
  showErrorLog () {
    let systemErrorLog = util.getErrorLog()
    let that = this
    wx.showModal({
      title: '错误日志',
      content: '是否复制错误日志到剪贴板？复制后将清空已有日志',
      confirmText: '复制',
      success (res) {
        if (!res.confirm) {
          return
        }
        wx.setClipboardData({
          data: JSON.stringify(systemErrorLog),
        })
        util.clearErrorLog()
        that.setData({
          showSystemErrorLog: false,
          detail_show: false
        })
      }
    })
  }
})